package com.cnhis.cloudhealth.clinical.hosnurse.hosdoctoradvice.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosdoctoradvice.dao.HosDoctorAdviceDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hosexpense.bo.HosExpenseBo;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class HosDoctorAdviceBo {
	@Resource
	private HosDoctorAdviceDao hosDoctorAdviceDao;
	@Resource
	private GetNewId getNewId;

	@Resource
	private HosExpenseBo hosExpenseBo;

	public ModelVo doctorAdviceSheetDiagnose(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			if ("2".equals(map.get("acf01").toString())) {
				list = hosDoctorAdviceDao.doctorAdviceSheetDiagnose(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private Map getlParaV_lPara262() throws Exception {
		return hosDoctorAdviceDao.getlParaV_lPara262();
	}

	public ModelVo getPatientsDoctorAdvice(Map map, int currentPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			Map m = getlParaV_lPara262();
			map.put("adate", DateUtils.getDateString(new Date()));
			map.putAll(m);
			pg = hosDoctorAdviceDao.getPatientsDoctorAdvice(map, currentPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}

		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setTotal(pg.getTotalNumber());
		mo.setRows(pg.getList());
		mo.setPage(currentPage);
		mo.setRecords(pg.getTotalNumber());
		mo.setTotalPage(pg.getTotalPage());
		return mo;
	}

	public ModelVo adviceHint(Map map, int currentPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {

			pg = hosDoctorAdviceDao.adviceHint(map, currentPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}

		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setTotal(pg.getTotalNumber());
		mo.setRows(pg.getList());
		return mo;
	}

	private Map getBCE03(Map map) throws Exception {
		return hosDoctorAdviceDao.getBCE03(map);
	}

	@SuppressWarnings({})
	public ModelVo execNurseShortcutAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		int lMode = Integer.parseInt(map.get("lMode").toString());
		int VCS05 = Integer.parseInt(map.get("vcs05").toString());
		Object obj = map.get("vcs06");
		String VCS06 = "";
		if (obj != null && !"".equals(obj)) {
			VCS06 = obj.toString();
		}
		String lDateA = DateUtils.getDateString(new Date());
		map.put("lDateA", lDateA);
		// 主键
		map.put("lCBM01", getNewId.nextId());// 医嘱单表
		map.put("lVAF01", getNewId.nextId());// 医嘱记录表
		map.put("lID", getNewId.nextId());// 住院诊断表
		map.put("VCS01", getNewId.nextId());// 病人出径记录
		map.put("lVCL01", getNewId.nextId());// 协作科室
		// 开嘱医师姓名、编号
		Map m = getBCE03(map);
		if (m != null && m.size() > 0) {
			map.put("lBCE03A", m.get("lbce03a"));
			map.put("lBCE02A", m.get("lbce02a"));
		}
		// 开单人姓名
		String lBCE03 = hosDoctorAdviceDao.getBCK03(map);
		map.put("lBCE03", lBCE03);
		// 科室id
		long BCK01B = hosDoctorAdviceDao.getBCK01(map);
		map.put("BCK01B", BCK01B);
		if (lMode == 0 || (lMode == 4 && VCS05 == 0)) {// 出院诊断记录
			Object lXml = map.get("lXml");
			Object lPatXml = map.get("lPatXml");
			List va02List = null;
			Map vae1Map = null;
			if (lXml != null && !"".equals(lXml)) {
				va02List = JsonUtil.getList(lXml.toString());
			}
			if (lPatXml != null && !"".equals(lPatXml)) {
				vae1Map = JsonUtil.getMap(lPatXml.toString());
			}
			map.put("va02List", va02List);
			map.put("vae1Map", vae1Map);
			// 修改病人登记信息
			hosDoctorAdviceDao.updateVAE1(map);
			// 添加病人诊断记录
			saveVAO2(map);
		}
		if (lMode == 3) {
			// 新增协作科室记录
			saveVCL1(map);
		}
		if (lMode == 4) {
			// 新增出径记录
			if (VCS05 == 1 && (VCS06 != null || !"".equals(VCS06))) {
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("异常退出路径，需要输入出径原因");
				return mo;
			}
			if (!existsVCN1(map)) {
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("此病人当前不在临床路径内");
				return mo;
			}
			// 限制病人出径记录、修改病人临床路径状态
			saveVCS1(map);
		}
		if (!(lMode == 4 && VCS05 == 1)) {
			// 新增医嘱单
			saveCBM2(map);
			// 新增医嘱
			saveVAF2(map);
			// 记录医嘱状态
			saveVBG1(map);
			// 增加医嘱提醒
			saveVBZ1(map);
		}
		return mo;
	}

	// 新增住院病人医嘱单
	private void saveCBM2(Map map) throws Exception {
		hosDoctorAdviceDao.saveCBM2(map);
	}

	// 新增住院病人医嘱记录
	private void saveVAF2(Map map) throws Exception {
		hosDoctorAdviceDao.saveVAF2(map);
	}

	// 新增住院病人医嘱记录
	private void saveVBG1(Map map) throws Exception {
		hosDoctorAdviceDao.saveVBG1(map);
	}

	// 新增住院病人医嘱提醒记录
	private void saveVBZ1(Map map) throws Exception {
		hosDoctorAdviceDao.saveVBZ1(map);
	}

	private boolean existsVCN1(Map map) throws Exception {
		return hosDoctorAdviceDao.existsVCN1(map) > 0;
	}

	private void saveVCS1(Map map) throws Exception {
		hosDoctorAdviceDao.saveVCS1(map);
		hosDoctorAdviceDao.updateVCN1(map);
	}

	private void saveVCL1(Map map) throws Exception {
		hosDoctorAdviceDao.saveVCL1(map);
	}

	private void saveVAO2(Map map) throws Exception {

		map.put("lID", getNewId.nextId());
		hosDoctorAdviceDao.insertVAO1(map);
		map.put("lID", getNewId.nextId());
		hosDoctorAdviceDao.insertVAO2(map);
	}

	public ModelVo storedNewTreamentRecord(Map map, int currentPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page page = null;
		try {
			page = hosDoctorAdviceDao.storedNewTreamentRecord(map, currentPage, pageSize);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(page.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(page.getList());
		return mo;
	}

	public ModelVo getItemsType(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getItemsType(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getAdviceProperty(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getAdviceProperty(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getAdviceItemsTree() throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getAdviceItemsTree();
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo queryAdviceItemsDetails(Map map, int currentPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page page = null;
		try {
			String aParamno = map.get("aParamno").toString();
			if ("0".equals(aParamno)) {
				page = hosDoctorAdviceDao.queryDetalParam0(map, currentPage, pageSize);
			} else {
				if ("3".equals(aParamno)) {
					if ("1".equals(map.get("lACF01").toString())) {
						page = hosDoctorAdviceDao.queryDetalParam1(map, currentPage, pageSize);
					} else if ("2".equals(map.get("lACF01").toString())) {
						page = hosDoctorAdviceDao.queryDetalParam2(map, currentPage, pageSize);
					}
				} else {
					page = hosDoctorAdviceDao.queryDetalParam(map, currentPage, pageSize);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(page.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(page.getList());
		return mo;
	}

	public ModelVo ruleAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.ruleAdvice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getDefaultLevel(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getDefaultLevel(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo queryTreatmentDetail(Map map, int currPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			pg = hosDoctorAdviceDao.queryTreatmentDetail(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		return mo;
	}

	public ModelVo queryMedicalAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.queryMedicalAdvice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getMedicalOffice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getMedicalOffice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getAdviceGroupDetail(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getAdviceGroupDetail(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo queryOffice() throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.queryOffice();
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo saveCASign(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.saveCASign(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private String getPara(Integer productId, Integer programId, Integer paramNo) throws Exception {
		Map hs = new HashMap();
		hs.put("productId", productId);
		hs.put("programId", programId);
		hs.put("paramNo", paramNo);
		return hosDoctorAdviceDao.getPara(hs);
	}

	public ModelVo fetchTreatment(Map map, int currPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			String para359_395 = "0";
			int lACF01 = Integer.parseInt(map.get("lACF01").toString());

			if (1 == lACF01) {
				para359_395 = getPara(100, 105002, 359);
			}

			if (2 == lACF01) {
				para359_395 = getPara(100, 105003, 395);

			}
			map.put("para359_395", para359_395);
			if (lACF01 >= 1) {
				if ("3".equals(map.get("aParamno").toString())) {
					if (1 == lACF01) {
						pg = hosDoctorAdviceDao.fetchTreatmentList1(map, currPage, pageSize);
					}

					if (2 == lACF01) {
						pg = hosDoctorAdviceDao.fetchTreatmentList2(map, currPage, pageSize);
					}
				}
				if ("0".equals(map.get("aParamno").toString())) {
					if ("1".equals(getPara(9999, 9999, 57))) {
						pg = hosDoctorAdviceDao.fetchTreatmentList01(map, currPage, pageSize);
					} else {
						pg = hosDoctorAdviceDao.fetchTreatmentList02(map, currPage, pageSize);
					}
				}
			} else {
				map.put("aParamno", "1");
				pg = hosDoctorAdviceDao.fetchTreatmentList(map, currPage, pageSize);
			}

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		return mo;
	}

	public ModelVo getHocusItems(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getHocusItems(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private String getABC021(Map map) throws Exception {
		return hosDoctorAdviceDao.getABC021(map);
	}

	private String getABC022(Map map) throws Exception {
		return hosDoctorAdviceDao.getABC022(map);
	}

	private Map getBBY25(Map map) throws Exception {
		return hosDoctorAdviceDao.getBBY25(map);
	}

	public ModelVo queryItemsTypeDetail(Map map, int currPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page pg = null;
		try {
			pg = hosDoctorAdviceDao.queryItemsTypeDetail(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		return mo;
	}

	public ModelVo medicalAdviceDetail(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			if ("1".equals(map.get("ACF01").toString())) {
				map.put("ABC02", getABC021(map));
			} else {
				map.put("ABC02", getABC022(map));
			}
			Map m = getBBY25(map);
			map.put("BBY08", m.get("bby08"));
			map.put("BAG057", m.get("bag057"));
			map.put("BBY25", m.get("bby25"));
			list = hosDoctorAdviceDao.medicalAdviceDetail(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private String paramABC02(Map map) throws Exception {
		return hosDoctorAdviceDao.paramABC02(map);
	}

	public ModelVo queryFeeDetail(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			map.put("ABC02", paramABC02(map));
			list = hosDoctorAdviceDao.medicalAdviceDetail(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private String params(Map map) throws Exception {
		return hosDoctorAdviceDao.params(map);
	}

	public ModelVo checkAdviceFeeDetail(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			map.put("ABC02", params(map));
			list = hosDoctorAdviceDao.checkAdviceFeeDetail(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo checkItemsBody(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.checkItemsBody(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo getBarcode(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.getBarcode(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo medicalInventory(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.medicalInventory(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo herbalMedicineAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.herbalMedicineAdvice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo herbalMedicineById(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.herbalMedicineById(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo herbalCommonList(Map map, int currPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page page = null;
		try {
			page = hosDoctorAdviceDao.herbalCommonList(map, currPage, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(page.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(page.getList());
		return mo;
	}

	public ModelVo queryAdviceItems(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.queryAdviceItems(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo checkMedicalRoom(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		String type = map.get("type").toString();
		try {
			if(type.equals("1")){
				list = hosDoctorAdviceDao.checkMedicalRoom(map);
			}else{
				list = hosDoctorAdviceDao.checkMedicalRoom1(map);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo tempAdvice(Map map, int currPage, int pageSize) throws Exception {
		ModelVo mo = new ModelVo();
		Page page = new Page();
		Map m = hosDoctorAdviceDao.getParameter(map);
		if (m != null) {
			map.put("VAA04", m.get("vaa04").toString());
			map.put("BCQ04B", m.get("bcq04b").toString());
		}
		try {
			page = hosDoctorAdviceDao.tempAdvice(map, currPage, pageSize);

		} catch (Exception e) {
			e.printStackTrace();
		}
		mo.setList(page.getList());
		mo.setRows(page.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		return mo;
	}

	private List getTempVaf(Map map) throws Exception {
		return hosDoctorAdviceDao.getTempVaf(map);
	}

	private boolean checkFee(Map map) throws Exception {
		return hosDoctorAdviceDao.checkFee(map) > 0;
	}

	private boolean checkOPS(Map map) throws Exception {
		return hosDoctorAdviceDao.checkOPS(map) > 0;
	}

	private boolean checkCurrentOPS(Map map) throws Exception {
		return hosDoctorAdviceDao.checkCurrentOPS(map) > 0;
	}

	private boolean checkVAF(Map map) throws Exception {
		return hosDoctorAdviceDao.checkVAF(map) > 0;
	}

	private void updateVafState(Map map) throws Exception {
		hosDoctorAdviceDao.updateVafState(map);
	}

	public ModelVo clinicAdviceBlankOut(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		List TmpVaf = getTempVaf(map);
		map.put("TmpVaf", TmpVaf);
		if (checkFee(map)) {
			vo.setResultMsg("临嘱对应费用还没冲销，请返回先冲销对应费用");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		if (checkOPS(map)) {
			vo.setResultMsg("手术临嘱对应医技记账费用还没冲销，请返回先冲销对应费用");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		if (checkCurrentOPS(map)) {
			vo.setResultMsg("手术室已经接收安排手术，不能在取消");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		if (checkVAF(map)) {
			vo.setResultMsg("临嘱已经取消，请返回重新选择");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		// 修改医嘱状态
		updateVafState(map);

		return vo;
	}

	public ModelVo checkAdviceStateContent(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosDoctorAdviceDao.checkAdviceStateContent(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo checkAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			map.put("pValue", hosDoctorAdviceDao.getParamValue());
			list = hosDoctorAdviceDao.checkAdvice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private Map getValue374_pValue() throws Exception {
		return hosDoctorAdviceDao.getValue374_pValue();
	}

	public ModelVo proofreadAdvice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List<Map<String, Object>> list = null;
		try {
			Map m = hosDoctorAdviceDao.getVAA04(map);
			if (m != null) {
				map.put("VAA04", m.get("vaa04"));
				map.put("BCQ04B", m.get("bcq04b"));
			}
			Map m1 = getValue374_pValue();
			map.putAll(m1);
			map.put("adate", DateUtils.getDateString(new Date()));
			list = hosDoctorAdviceDao.proofreadAdvice(map);

			mo.setList(list);
			mo.setRows(list);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	private boolean isDelete(Map map) throws Exception {
		return hosDoctorAdviceDao.isDelete(map) > 0;
	}

	private boolean isCurrented(Map map) throws Exception {
		return hosDoctorAdviceDao.isCurrented(map) > 0;
	}

	private boolean checkOffice(Map map) throws Exception {
		return hosDoctorAdviceDao.checkOffice(map) > 0;
	}

	private boolean checkValuation(Map map) throws Exception {
		return hosDoctorAdviceDao.checkValuation(map) > 0;
	}

	private void saveVBD2(Map map) throws Exception {
		hosDoctorAdviceDao.saveVBD2(map);
	}

	private void saveVBG2(Map map) throws Exception {
		hosDoctorAdviceDao.saveVBG2(map);
	}

	private List getTempVaf2(Map map) throws Exception {
		List<Map<String, Object>> ls = hosDoctorAdviceDao.getTempVaf2(map);
		for (Map<String, Object> mm : ls) {
			mm.put("vbg01", getNewId.nextId());
		}
		return ls;
	}

	// 修改医嘱为校对
	private void updateVAF2(Map map) throws Exception {
		hosDoctorAdviceDao.updateVAF2(map);
	}

	public ModelVo execNurseAdviceProofread(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String json = map.get("lXML").toString();
		JSONObject jsonObject = JSONObject.fromObject(json);
		List<Map<String, Object>> tmpVAF = JsonUtil.getList(jsonObject.getJSONArray("VAF1").toString());
		// 获取计价信息
		String vaf06 = map.get("vaf06").toString();
		String VAFID = map.get("VAFID").toString();
		HashMap hs = new HashMap();
		hs.put("ACF01", 2);
		hs.put("lVAF06", Long.parseLong(vaf06));
		hs.put("VAFID", VAFID);
		List<Map<String, Object>> tmpVBD = (List<Map<String, Object>>) hosExpenseBo.validationAdviceValuation(hs)
				.getList();

		// List<Map<String, Object>> tmpVBD=
		// JsonUtil.getList(jsonObject.getJSONArray("VBD1").toString());
		for (Map<String, Object> m : tmpVAF) {
			Integer vaf10 = MapUtils.getInteger(m,"vaf10",3);
			if (m.containsKey("vaf10")&&vaf10.intValue()!=2) {
				m.put("vaf10", 3);
			}
			if (m.containsKey("vaf30")) {
				m.put("vaf30", "09");
			}
			Map<String, Object> vaf2map = getvaf2(m);
			int size = selectvbm1(vaf2map);
			if(size==0){
				vo.setResult(StaticKeys.OH_FALSE);
				vo.setResultMsg(MapUtils.getString(vaf2map,"vaf22")+" 的病人医嘱单数据没有写入,请删除此条医嘱并重新开这条医嘱!");
				return vo;
			}
			size = selectEdit(vaf2map);
			if(size>0){
				vo.setResult(StaticKeys.OH_FALSE);
				vo.setResultMsg("医嘱 "+MapUtils.getString(vaf2map,"vaf22")+" 正在编辑中,不能进行校队.请等待医生编辑完成后,刷新医嘱重新校队!");
				return vo;
			}
		}
		map.put("tmpVAF", tmpVAF);
		map.put("tmpVBD", tmpVBD);
		map.remove("lXML");

		if (isDelete(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("有医嘱已经被删除,不能在校对，请返回刷新");
			return vo;
		}
		if (isCurrented(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("已有医嘱被校对或执行，不能在校队，请返回刷新");
			return vo;
		}
		if (checkOffice(map) && tmpVBD.size() != 0) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("医嘱计价中执行科室不正确，请检查修改");
			return vo;
		}
		tmpVAF = getTempVaf2(map);
		map.put("tmpVAF", tmpVAF);
		if (checkValuation(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("药品医嘱没有计价内容，不能校队，请先联系管理员");
			return vo;
		}

		if (tmpVBD.size() != 0) {
			List<Map<String, Object>> vbd1List = hosDoctorAdviceDao.getVBD2List(map);
			if (vbd1List != null && vbd1List.size() != 0) {
				int size = vbd1List.size();
				for (int i = 0; i < size; i++) {
					Map<String, Object> mm = vbd1List.get(i);
					mm.put("vbd01", getNewId.nextId());
				}
				map.put("VBD2List", vbd1List);
				// 保存医嘱计价信息
				saveVBD2(map);
			}
		}

		// 修改医嘱为校对
		updateVAF2(map);

		// 保存医嘱状态
		saveVBG2(map);
		//修改消息中的医嘱状态
		map.put("vae01", "%"+vaf06+"%");
		Integer num = updatemassager(map);
		vo.setResultMsg(num.toString());
		return vo;
	}

	private Integer updatemassager(Map map) throws Exception  {
		// TODO Auto-generated method stub
		hosDoctorAdviceDao.updatemassager(map);
		return hosDoctorAdviceDao.selectmassager(map);
	}

	private boolean checkState(Map map) throws Exception {
		return hosDoctorAdviceDao.checkState(map) > 0;
	}

	private void insertVDD1(Map map) throws Exception {
		map.put("vdd03", getNewId.nextId());
		hosDoctorAdviceDao.insertVDD1(map);
	}

	public ModelVo execMedicalToZero(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String json = map.get("lxml").toString();
		JSONObject jsonObject = JSONObject.fromObject(json);
		List tmpVDD = JsonUtil.getList(jsonObject.getJSONArray("VDD1").toString());
		map.put("tmpVDD", tmpVDD);
		if (checkState(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("对应费用已经发药，不能在修改领药数量，返回刷新数据重新调整");
			return vo;
		}
		// 新增请领记录
		insertVDD1(map);
		return vo;
	}
	public int selectvbm1 (Map map) throws Exception{		
		return hosDoctorAdviceDao.selectvbm1(map);		
	}
	public int selectEdit (Map map) throws Exception{
		return hosDoctorAdviceDao.selectEdit(map);
	}
	public Map getvaf2 (Map map) throws Exception{		
		return hosDoctorAdviceDao.getvaf2(map);		
	}
}
